package com.ai.langchain4j.tools;

import com.ai.langchain4j.entity.Appointment;
import com.ai.langchain4j.entity.DoctorSchedule;
import com.ai.langchain4j.entity.ScheduleTimeSlot;
import com.ai.langchain4j.mapper.DoctorScheduleMapper;
import com.ai.langchain4j.mapper.ScheduleTimeSlotMapper;
import com.ai.langchain4j.service.AppointmentService;
import com.alibaba.dashscope.utils.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 林
 * @create 2025-07-19 17:44
 */
@Component
public class ScheduleInfoTools {

    @Resource
    private DoctorScheduleMapper doctorScheduleMapper;

    @Resource
    private ScheduleTimeSlotMapper scheduleTimeSlotMapper;

    @Tool(name = "查看医生的排班时间",
            value = "根据参数，查询这个医生的上班坐诊时间。")
    public String bookAppointment(DoctorSchedule doctorSchedule) {
//查找数据库中是否包含对应的预约记录
        QueryWrapper<DoctorSchedule> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("doctor_name", doctorSchedule.getDoctorName());
        List<DoctorSchedule> doctorSchedules = doctorScheduleMapper.selectList(objectQueryWrapper);

        if (CollectionUtils.isEmpty(doctorSchedules)) {
            return "没有查询到医生信息";
        }
        for (DoctorSchedule schedule : doctorSchedules) {
            QueryWrapper<ScheduleTimeSlot> objectQueryWrapper1 = new QueryWrapper<>();
            objectQueryWrapper1.eq("schedule_id", schedule.getId());
            List<ScheduleTimeSlot> scheduleTimeSlots = scheduleTimeSlotMapper.selectList(objectQueryWrapper1);
            schedule.setTimeSlots(scheduleTimeSlots);
        }
        return JsonUtils.toJson(doctorSchedules);
    }

}

